package org.neuedu.fly.util;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.ResourceBundle;

public class SendSmsUtil {

    private static ResourceBundle rb = ResourceBundle.getBundle("sms");


    public static String sendSms(String tel,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", rb.getString("accessKeyId"), rb.getString("secret"));
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", rb.getString("signName"));
        request.putQueryParameter("TemplateCode", rb.getString("templateCode"));
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
}

    public static void main(String[] args) {
        String code = String.valueOf(new Double(Math.random()*10000).intValue());
        String str = SendSmsUtil.sendSms("18384777803",code);
        System.out.println(str);
    }

}
